Practical 4 - Full Version

Table of Contents

Defining Different Types of Agent
Agent-specific variables
Creating the visitors
Making the visitors move
print

Practical 4 - Modelling a Music Festival


Through the previous practical classes you now have all the basic knowledge you'll need to create quite advanced models in NetLogo. For the remaining three practical sessions we will work on a model of a festival. This is also the model that you can use for your projects. Here's the scenario:

The Scenario

You have been approached by the organisers of a music festival who are concerned with the amount of crime that takes place there. They have asked you to create a model of the festival and suggest ways in which they could intervene to prevent people from being mugged. The festival site consists of two stages (a main stage and secondary stage) as well as a mixture of burger stands, beer tents and vegetarian food stalls.

You will create a model that is able to do the following:

    To save time, you have been provided with a skeleton model to work with. The festival environment has been created, but otherwise the model doesn't do anything.

    The basic model environment:
  1. Download the basic model from here: festival.nlogo.
  2. Important: to force the browser to download the file you might need to right-click on the link and choose Save link as ...

  3. Save the model somewhere sensible with your other work for this module.
  4. Open the model in NetLogo and check that it works by pressing 'Setup'. You should see something like the right image. You can run the model, but as there aren't any agents in there yet nothing will actually happen.
  5. Defining Different Types of Agent

  6. Now have a look at the model code.
  7. There is a command at the start that you might not recognise: breed [visitors visitor] Up to now, we have always just referred to turtles when we want to work with the agents in a model. This is fine when you only have one type of agent, but later we will have two types ('visitors' and 'muggers') so we need a way to differentiate between them.

    NetLogo includes the breed command to make this possible. In the code above, we tell NetLogo that we will create a specific type of agent referred to as 'visitors' (plural) and 'visitor' (when there is only one of them).

    After writing the breed command it is possible to access just the visitor agents with: ask visitors [ .. ]

    Agent-specific variables

    Having created visitor agents, we can now create variables that are unique to visitors. We can use agent-specific variables to store pieces of information that are unique to a particular agent type. In this model, our visitors will walk around the festival area, so they need a variable that will keep track of where they are going.

  8. Add the following just below the breed line: visitors-own[
      destination
    ]
    This creates a new variable called destination that our visitors can use to keep track of their current destination (a burger stand, the main stage, a veggie food stall, etc.).
  9. Creating the visitors

    Next, we need to make some minor improvements to the model to create the visitor agents.

  10. Create a new slider that will tell NetLogo how many visitors you want to include in the model. You can give the slider any name you choose; in the examples below I have called it 'number-of-visitors'. You can also add as many visitors as you like; I set the initial value to be 100 with a maximum of 500. (If you have forgotten how to make a slider, refer back to the sliders section of Practical 2).
  11. Create a new procedure called 'setup-people' that will create the visitors. We have created code to do a similar job already, for example see the setup-turtles procedure in practical 2. You can use the code below to help you create the procedure - remember that anything after a ';' is a comment and ignored by NetLogo. You will have to write the real code in place of the comments. to setup-people
      create-visitors number-of-visitors [
        ;set the shape of the new visitor to "person"
        ;set the colour of the visitor to gray
        ;give the person a random (x,y) coordinate
        ;This last bit sets the visitor's 'destination' variable
        set destination one-of patches with [
          pcolor = yellow or pcolor = green or pcolor = blue or pcolor = orange or pcolor = red
        ]
      ]
    end
  12. The last thing that the code above does is set each visitor's destination variable. This is the patch that the person is going to walk to as they move about the festival. We choose any patch that is either a beer stand (yellow), veggie food stand (green), burger van (blue), small stage (orange) or big stage (red).

    Visitors' decision process
  13. Finally, to make the procedure you have just created run, add the line setup-people to the 'setup' procedure. (Remember that NetLogo will run the setup procedure when someone presses the 'Setup Model' button).
  14. Check that this works but going back to the 'Interface' tab and clicking on 'Setup Model' - you should see the visitors created.
  15. Making the visitors move

    The last thing to do to finish the basic model is to add a few lines of code so that the agents actually move around from stall to stall. The diagram on the right illustrates the rules that control the behaviour of the visitors (i.e. their 'thought process').

    At each iteration, the visitors check to see if they have reached their destination by comparing the patch that they are currently standing on to the one they are trying to get to (if ( patch-here = destination ) ). If they have reached their destination patch, then they choose a new (random) destination to travel to. Then they move forward towards their destination.

  16. Add the following code to the 'move' procedure (which is empty at the moment). if ( patch-here = destination ) [
      set destination one-of patches with [
        pcolor = yellow or pcolor = green or pcolor = blue or pcolor = orange or pcolor = red
      ]
    ]
    face destination
    forward 1
  17. Now if you go back to the model interface and press 'Run Model' the visitors should start travelling between the different stalls and the two stages. Slow the model down if it is running too quickly to make sense of.

That's all for the this practical, you now have a simple model with some virtual people wandering around a music festival. This probably wont have taken very long, so complete the activities below. Then, if you have the energy, have a look at Practical 5.

Activities

  1. Note down the code that you would use to create a new type of agent called 'managers'.
    Command:
  2. What command would you use to create a new variable called 'salary' that is unique to the 'manager' agents?
    Command:
  3. What commands would you use to set the salary of the agents standing at coordinates (4,7) to £10,000.
    Hint: have a look at the documentation for the turtles-on command.
    Command: